package com.itheima.bos.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itheima.bos.dao.CourierRepository;
import com.itheima.bos.dao.FixedAreaRepository;
import com.itheima.bos.domain.base.Courier;
import com.itheima.bos.domain.base.FixedArea;
import com.itheima.bos.domain.base.TakeTime;
import com.itheima.bos.service.FixedAreaService;

@Service
@Transactional
public class FixedAreaServiceImpl implements FixedAreaService {

	@Autowired
	private FixedAreaRepository fixedAreaRepository;

	@Autowired
	private CourierRepository courierRepository;
	
	@Override
	public void save(FixedArea model) {
		fixedAreaRepository.save(model);
	}

	@Override
	public Page<FixedArea> pageQuery(Specification<FixedArea> spec, Pageable pageable) {
		return fixedAreaRepository.findAll(spec, pageable);
	}

	@Override
	public void associationCourierToFixedArea(String id, String courierId, String takeTimeId) {
		//将快递员与收派时间关联
		Courier courier = courierRepository.findOne(Integer.parseInt(courierId));
		TakeTime takeTime = new TakeTime();
		takeTime.setId(Integer.parseInt(takeTimeId));
		courier.setTakeTime(takeTime);
		
		//将定区与快递员关联
		FixedArea fixedArea = fixedAreaRepository.findOne(id);
		fixedArea.getCouriers().add(courier);
	}

	@Override
	public List<Courier> associationCourier(String id) {
		return courierRepository.findByFixedAreasId(id);
	}

}
